/**
 * 
 */
package com.et114.modules.products.interfaces;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;

import com.et114.core.services.impl.BaseServicesImpl;
import com.et114.modules.products.card.services.CardServiceImpl;
import com.et114.modules.products.card.vo.Card;
import com.et114.modules.products.card.vo.CardServicePlan;
import com.et114.modules.products.card.vo.CardType;
import com.et114.modules.products.cellphone.services.CellPhoneServiceImpl;
import com.et114.modules.products.cellphone.vo.CellPhone;
import com.et114.modules.products.commons.Const;
import com.et114.modules.products.digitalTv.services.DigitalTvServiceImpl;
import com.et114.modules.products.digitalTv.vo.DigitalTv;
import com.et114.modules.products.digitalTv.vo.DigitalTvSpeedType;
import com.et114.modules.products.netbook.services.NetBookServiceImpl;
import com.et114.modules.products.securitydoor.services.SecuritySystemServiceImpl;
import com.et114.modules.products.securitydoor.vo.SecurityDoor;
import com.et114.modules.products.securitydoor.vo.SecuritySystemType;
import com.et114.modules.system.dictionary.DictionaryCache;
import com.et114.modules.system.dictionary.vo.BizOption;

/*
 * @author guanhw
 */
public class ProInterface extends BaseServicesImpl {
	public static final String PROID_KEY = "proId" ; 
	public static final String PROTYPE_KEY = "proType" ; 
	public static final String PROCOLOR_KEY = "color" ; 
	public static final String PROCOLOR_NAME_KEY = "colorName" ; 
	public static final String PROSERVICEID_KEY = "proServiceId" ;
	public static final String COUNTRY_KEY = "COUNTRY" ;
	
	private CellPhoneServiceImpl cellPhoneServiceImpl ;  
	private NetBookServiceImpl netBookServiceImpl ;
	private CardServiceImpl cardServiceImpl ;  
	private DigitalTvServiceImpl digitalTvServiceImpl ; 
	private SecuritySystemServiceImpl	securitySystemServiceImpl;

	
	public void setCellPhoneServiceImpl ( CellPhoneServiceImpl cellPhoneServiceImpl ) {
		this.cellPhoneServiceImpl = cellPhoneServiceImpl;
	}

	public void setNetBookServiceImpl ( NetBookServiceImpl netBookServiceImpl ) {
		this.netBookServiceImpl = netBookServiceImpl;
	}

	public void setCardServiceImpl ( CardServiceImpl cardServiceImpl ) {
		this.cardServiceImpl = cardServiceImpl;
	}

	public void setDigitalTvServiceImpl ( DigitalTvServiceImpl digitalTvServiceImpl ) {
		this.digitalTvServiceImpl = digitalTvServiceImpl;
	}

	public void setSecuritySystemServiceImpl (
			SecuritySystemServiceImpl securitySystemServiceImpl ) {
		this.securitySystemServiceImpl = securitySystemServiceImpl;
	}

	@ SuppressWarnings ( "unchecked" )
	public Object getPro( Map< String , Object > map ) throws Exception {
		//
		logger.info ( "proId:" + map.get ( PROID_KEY ) + ";proType:"
						+ map.get ( PROTYPE_KEY ) + ";color:"
						+ map.get ( PROCOLOR_KEY ) + ";colorName:"
						+ map.get ( PROCOLOR_NAME_KEY ) + ";PROSERVICEID;"
						+ map.get ( PROSERVICEID_KEY ) + ";Lan:" + map.get ( COUNTRY_KEY ));
		//
		Assert.notNull ( map.get ( PROTYPE_KEY )  ) ;
		Assert.notNull ( map.get ( COUNTRY_KEY )  ) ;
		Assert.notNull ( map.get ( PROTYPE_KEY )  ) ;


		if ( !isNumeric( map.get ( PROTYPE_KEY ).toString ( ) ) ) 
			return null ; 
		
		if ( !isNumeric( map.get ( PROID_KEY ).toString ( ) ) ) 
			return null ; 
	
		
		Byte proType = Byte.valueOf ( map.get ( PROTYPE_KEY ).toString ( ) ) ; 
		if ( Const.PRO_TYPE_CART == proType ) {
			Assert.notNull ( map.get ( PROID_KEY )  ) ; 
			List < BizOption > bsspOps = DictionaryCache.getInstance ( )
					.listBizOptions ( Const.DIC_B_PRO_CARD_SERVICE_PLAN ,
							false , String.valueOf ( map.get ( COUNTRY_KEY ) ) );
			List < BizOption > bssOps = DictionaryCache.getInstance ( )
					.listBizOptions ( Const.DIC_B_PRO_CARD_TYPE , false ,
							String.valueOf ( map.get ( COUNTRY_KEY ) ) );

			Card card =  cardServiceImpl.getBaseCardById4View ( Long.valueOf ( map
					.get ( PROID_KEY ).toString ( ) ) , null ) ; 
			
			for ( BizOption bioCt : bssOps ) {
				if ( Byte.valueOf ( bioCt.getOptionValue ( ) ) .equals ( 
						card.getCardType ( ) ) ) {
					CardType cardTypeVo = new CardType ( );
					cardTypeVo.setCardTypeId ( Byte.valueOf ( bioCt
							.getOptionValue ( ) ) );
					cardTypeVo.setCardTypeName ( bioCt.getOptionName ( ) );
					card.setCardTypev ( cardTypeVo );
					break;
				}
			}
			
			for ( BizOption bizop : bsspOps ) {
				if (  Byte.valueOf (  bizop.getOptionValue ( ) ) .equals ( 
						card.getCardPlan ( ) ) ) {
					CardServicePlan cardServicePlanv = new CardServicePlan ( );
					cardServicePlanv.setCardServicePlanId ( Byte
							.valueOf ( bizop.getOptionValue ( ) ) );
					cardServicePlanv.setCardServicePlanName ( bizop
							.getOptionName ( ) );
					card.setCardServicePlanv ( cardServicePlanv );
					break;
				}
			}
			
			return card ; 
		}
		else if ( Const.PRO_TYPE_TV == proType  ) { 
			Assert.notNull ( map.get ( PROID_KEY )  ) ; 
			List < BizOption > bssOps = DictionaryCache.getInstance ( )
					.listBizOptions ( Const.DIC_B_PRO_DT_TYPE , false ,
							String.valueOf ( map.get ( COUNTRY_KEY ) )  );
			
			DigitalTv digitalTv =  digitalTvServiceImpl.getBasetDigitalTvById4View ( Long.valueOf ( map
					.get ( PROID_KEY ).toString ( ) ) , null  ) ; 
			
			for ( BizOption bioCt : bssOps ) {
				if ( Byte.valueOf ( bioCt.getOptionValue ( ) ) .equals ( 
						digitalTv.getdSpeedTypeTypeId ( ) ) ) {
					DigitalTvSpeedType digitalTvSpeedType = new DigitalTvSpeedType( ) ;
					digitalTvSpeedType.setdSpeedTypeTypeId ( Byte.valueOf ( bioCt.getOptionValue ( ) ) ) ; 
					digitalTvSpeedType.setDigitalTvTypeName ( bioCt.getOptionName ( ) ) ; 
					digitalTv.setDigitalTvSpeedType ( digitalTvSpeedType ) ; 
					break ; 
				}
			}
			
			return digitalTv ; 
		}
		else if ( Const.PRO_TYPE_SS == proType  ) { 
			Assert.notNull ( map.get ( PROID_KEY )  ) ; 
			List < BizOption > bssOps = DictionaryCache.getInstance ( )
					.listBizOptions ( Const.DIC_B_SS_Type_KEY , false ,
							String.valueOf ( map.get ( COUNTRY_KEY ) ) );
			
			SecurityDoor sd =  securitySystemServiceImpl.getBaseSecuritySystemById4View ( Long.valueOf ( map
					.get ( PROID_KEY ).toString ( ) ) , null  ) ; 
			
			for ( BizOption bioCt : bssOps ) {
				if ( Byte.valueOf ( bioCt.getOptionValue ( ) ) .equals ( 
						sd.getSdtypeId ( ) ) ) {
					SecuritySystemType ssType = new SecuritySystemType( ) ; 
					ssType.setSdTypeId ( Byte.valueOf ( bioCt.getOptionValue ( ) ) ) ; 
					ssType.setSdTypeName ( bioCt.getOptionName ( ) ) ;
					sd.setSsType ( ssType ) ;
					break ; 
				}
			}						
			return sd ; 
		}
		
		else if ( Const.PRO_TYPE_CELLPHONE == proType ) {
			Assert.notNull ( map.get ( PROCOLOR_KEY )   ) ; 
			Assert.notNull ( map.get ( PROCOLOR_NAME_KEY )   ) ; 
			Assert.notNull ( map.get ( PROSERVICEID_KEY )   ) ; 
			
	
			if ( ! isNumeric ( map.get ( PROCOLOR_KEY ).toString ( ) ) )
				return null;
			if ( StringUtils.isEmpty ( map.get ( PROCOLOR_NAME_KEY ) .toString ( ) )
					|| StringUtils.isBlank ( map.get ( PROCOLOR_NAME_KEY ) .toString ( ) ) )
				return null;
			if ( ! isNumeric ( map.get ( PROSERVICEID_KEY ) .toString ( ) ) )
				return null;
			
			
			return cellPhoneServiceImpl.getOrderCellPhone ( Long.valueOf ( map
					.get ( PROID_KEY ).toString ( ) ) , Byte.valueOf ( map
					.get ( PROCOLOR_KEY ).toString ( ) ) , Long.valueOf ( map
					.get ( PROSERVICEID_KEY ).toString ( ) ) , String.valueOf ( map
					.get ( COUNTRY_KEY ) ) );
		}
		else if ( Const.PRO_TYPE_NETBOOK== proType ) {
			Assert.notNull ( map.get ( PROCOLOR_KEY )   ) ; 
			Assert.notNull ( map.get ( PROCOLOR_NAME_KEY )   ) ; 
			Assert.notNull ( map.get ( PROSERVICEID_KEY )   ) ; 
			
			if ( ! isNumeric ( map.get ( PROCOLOR_KEY ).toString ( ) ) )
				return null;
			if ( StringUtils.isEmpty ( map.get ( PROCOLOR_NAME_KEY ) .toString ( ) )
					|| StringUtils.isBlank ( map.get ( PROCOLOR_NAME_KEY ) .toString ( ) ) )
				return null;
			if ( ! isNumeric ( map.get ( PROSERVICEID_KEY ) .toString ( ) ) )
				return null;
			
			return netBookServiceImpl.getOrderNetBook ( Long.valueOf ( map
					.get ( PROID_KEY ).toString ( ) ) , Byte.valueOf ( map
							.get ( PROCOLOR_KEY ).toString ( ) ) , Long.valueOf ( map
							.get ( PROSERVICEID_KEY ).toString ( ) ) , String.valueOf ( map
							.get ( COUNTRY_KEY ) )  ) ;
		}
		else return null ; 
	}
	
	
	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}	
	
	public static void main( String[] ss ) {
		ProInterface pi = new ProInterface( ) ;
		Map< String , Object > map = new HashMap< String , Object >( ) ; 
		map.put ( PROTYPE_KEY , Const.PRO_TYPE_CELLPHONE  ) ;
		map.put ( PROID_KEY , Long.valueOf ( 26 )  ) ;
		map.put ( PROCOLOR_KEY , 1  ) ;
		map.put ( PROCOLOR_NAME_KEY , "黑色"  ) ;
		map.put ( PROSERVICEID_KEY , Long.valueOf ( 26 )  ) ;
		try {
			Object obj = pi.getPro ( map ) ; 
			CellPhone cellPhone = ( CellPhone ) obj ; 
			System.out.println ( cellPhone.getProSupplierId ( ) ) ;
		}
		catch ( Exception ex ) {
			ex.printStackTrace ( ) ; 
		}
		
	}
}
